package com.cares.core.mng.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cares.core.base.controller.BaseController;
import com.cares.core.mng.service.OrgService;
import com.cares.core.utils.Constant;
import com.cares.core.utils.StringUtil;
import com.cares.entity.mng.MenuOperate;
import com.cares.entity.mng.Orgnization;

import com.alibaba.fastjson.JSON;

/**
 * LoginController负责打开登录页面(GET请求)和登录出错页面(POST请求)，
 * 
 * 真正登录的POST请求由Filter完成,
 * 
 * @author calvin
 */
@Controller
@RequestMapping(value = "/Mng/org")
public class OrgController extends BaseController {

	@Autowired
	@Qualifier("orgService")
	OrgService orgService;

	private Map<String, String> map = new HashMap<String, String>();

	@RequestMapping(method = RequestMethod.GET)
	public String mainPage() {
		return "frm/mng/org/orgTreeGrid";
	}

	@RequestMapping(value = "toAddOrg")
	public String toAddOrg(HttpServletRequest req) {
		String id = req.getParameter("id");
		if (StringUtil.isNotEmpty(id)) {
			req.setAttribute("DATA", JSON.toJSON(orgService.get(id,true)));
		}
		return "frm/mng/org/orgAdd";
	}

	/**
	 * Ajax请求
	 */
	@RequestMapping(value = "updateOrg")
	public String updateOrg(Orgnization org, HttpServletRequest req,
			HttpServletResponse response) {
		try {
			int result  =orgService.saveOrUpdateOrg(org);
			if(result==2){
				req.setAttribute("infoMsg", "机构代码重复！");
				req.setAttribute("callJS", "history.back(-1);");
			}
		} catch (Exception ex) {
			req.setAttribute("infoMsg", "系统异常！请联系管理员 <br/> "+ex.getMessage());
			ex.printStackTrace();
		}

		return SUCCESS_PAGE;
	}


	/**
	 * Ajax请求
	 */
	@RequestMapping(value = "treeGridData")
	@ResponseBody
	public void treeGridData(Orgnization org, HttpServletRequest req,
			HttpServletResponse response) {
		String id = req.getParameter("id");
		List<Map<String, Object>> itemsList = new ArrayList<Map<String, Object>>();
		if (StringUtil.isEmpty(id)) {
			if (orgService.isPermmit(Constant.SUPER_KEY)) {
				itemsList = orgService.orgTree(new Long(Constant.ORG_NODE_SYS_TOP));
			}else{
				Orgnization userOrg = orgService.get(orgService.getCurrentUser()
						.getOrgId());
				orgService.makeNode(orgService.getCurrentUser().getOrgId(),
						itemsList, userOrg);
			}			
		} else {
			/*Orgnization curOrg = orgService.get(new Long(id));
			if(null!=curOrg){//越级访问
				Orgnization userOrg = orgService.get(orgService.getCurrentUser()
					.getOrgId());
				System.out.println(userOrg.getOrgPath()+"//"+curOrg.getOrgPath()+"="+curOrg.getOrgPath().indexOf(userOrg.getOrgPath()));
				if(curOrg.getOrgPath().indexOf(userOrg.getOrgPath())==-1){//未授权！
					return;
				}
			}*/
			itemsList = orgService.orgTree(new Long(id));

		}
		String json = JSON.toJSONString(itemsList);
		super.outString(response, json);

	}

	/**
	 * Ajax请求
	 */
	@RequestMapping(value = "treeData")
	@ResponseBody
	public void treeData(Orgnization org, HttpServletRequest req,
			HttpServletResponse response) {
		Long nodeId = new Long("1");
		List<Map<String, Object>> itemsList = orgService.orgTree(nodeId, 1);

		String json = JSON.toJSONString(itemsList);
		super.outString(response, json);

	}
}
